package com.movitech.mobile.cz.base.common.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Created by Blue.Xia on 2018/3/12.
 */
public class DoubleUtils {

    public static String format(Double d) {
        if (d == null) {
            d = 0.0;
        }
        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(d);
    }

    public static String format(Double d, Integer length) {
        if (d == null) {
            d = 0.0;
        }
        if (length == null) {
            length = 2;
        }
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(length);
        return nf.format(d);
    }

    public static void main(String[] args) {
        System.out.println(DoubleUtils.format(12.23565, 3));
    }
}
